package models

import (
	"Wei-Xin/weixinrpc/weixinrpc"

	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Account   string `gorm:"type:char(11);not null;"`
	Password  string `gorm:"type:char(32);not null;"`
	HeadImage string `gorm:"type:varchar(500);not null;"`
	NickName  string `gorm:"type:varchar(50);not null;"`
	Address   string `gorm:"type:varchar(50);not null;"`
}

type Moment struct {
	gorm.Model
	Content string `gorm:"type:varchar(300);not null;"`
	UserId  int    `gorm:"type:int(11);not null;"`
}

func (m *Moment) MomentList(db *gorm.DB, in *weixinrpc.MomentListRequest) ([]*weixinrpc.MomentList, error) {

	var lists []*weixinrpc.MomentList

	err2 := db.Debug().Model(&Moment{}).
		Select("users.id AS user_id,moments.id AS moment_id , users.nick_name , users.head_image , moments.content").
		Joins("LEFT JOIN users ON users.id = moments.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error

	return lists, err2
}
